import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;

import javax.swing.*;

public class GUI extends JPanel{
	private OrdenaAlumnos alumnosCar;
	private OrdenaAlumnos alumnosNac;
	private JButton examinar;
	private JButton ordenar;
	private JTextField ruta;
	private JLabel mostrarC;
	private JLabel mostrarN;
	private JScrollPane scrollC;
	private JScrollPane scrollN;
	
	public GUI(){
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		ruta = new JTextField();
		ruta.setPreferredSize(new Dimension(500,20));	
		examinar = new JButton("Examinar...");
		examinar.addActionListener(new ExaminarListener());
		ordenar = new JButton("Aceptar");
		ordenar.addActionListener(new OrdenarListener());
		mostrarC = new JLabel("<html>");
		mostrarC.setPreferredSize(new Dimension(250,250));	
		mostrarC.setVerticalAlignment(JLabel.TOP);
		mostrarN = new JLabel("<html>");
		mostrarN.setPreferredSize(new Dimension(250,250));		
		mostrarN.setVerticalAlignment(JLabel.TOP);	
		scrollC = new JScrollPane(mostrarC, 
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollN = new JScrollPane(mostrarN, 
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		revalidar();
	}
	
	public void revalidar(){
		removeAll();
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
		panel1.add(examinar);
		panel1.add(ordenar);
		panel2.add(scrollC);
		panel2.add(scrollN);
		add(ruta);
		add(panel1);
		add(panel2);
		revalidate();
	}
	
	private void mostrarNac(){
		mostrarN.setText("<html>");
		Alumno alumno = alumnosNac.mostrarNac();
		while (alumno != null){
			Format formato = new SimpleDateFormat("MM/dd/yyyy");
			mostrarN.setText(mostrarN.getText()+formato.format(alumno.getNac())
					+", "+alumno.getNombre()+"<br>");
			alumno = alumnosNac.mostrarNac();
		}		
		mostrarN.setText(mostrarN.getText()+"</html>");
		revalidar();
	}
	
	private void mostrarCarnet(){
		mostrarC.setText("<html>");
		Alumno alumno = alumnosCar.mostrarCarnet();
		while (alumno != null){
			mostrarC.setText(mostrarC.getText()+alumno.getCarnet()
					+", "+alumno.getNombre()+"<br>");
			alumno = alumnosCar.mostrarCarnet();
		}
		mostrarC.setText(mostrarC.getText()+"</html>");
		revalidar();
	}
	
	private class ExaminarListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			JFrame frame = new JFrame();
			FileDialog archivos = new FileDialog(frame, "Seleccionar Archivo", FileDialog.LOAD);
		    archivos.setDirectory("C:\\");
		    archivos.setLocation(50, 50);
			archivos.setVisible(true);  

			
			ruta.setText(archivos.getDirectory()+archivos.getFile());
		}
		
	}
	
	
	private class OrdenarListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			try {
				alumnosCar = new OrdenaAlumnos(new CarneComparator(), ruta.getText());
				alumnosNac = new OrdenaAlumnos(new DateComparator(), ruta.getText());
				mostrarCarnet();
				mostrarNac();
			} catch (IOException e1) {
			}
		}
		
	}
}
